package com.lzx.shop.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.lzx.shop.dataobject.CategoryDO;
import com.lzx.shop.dataobject.ProductDO;
import com.lzx.shop.dataobject.UserDO;
import com.lzx.shop.model.Paging;
import com.lzx.shop.model.Product;
import com.lzx.shop.model.Result;
import com.lzx.shop.params.ProductParam;
import com.lzx.shop.service.CategoryService;
import com.lzx.shop.service.ProductService;
import com.lzx.shop.service.StoreService;
import com.lzx.shop.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName ProductApi.java
 * @Description TODO
 * @createTime 2021年03月21日 13:45:00
 */
@Api(tags = "ProductApidd", description = "商品管理")
@RestController
@RequestMapping("/product")
public class ProductApi {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private StoreService storeService;

    @ApiOperation("查询所有商品")
    @GetMapping("/list")
    public Result list(@RequestParam(value = "page",required = false)Integer page,@RequestParam(value="limit",required = false)Integer limit) {
        long begin = System.currentTimeMillis();
        System.out.println();

/*
        com.github.pagehelper.Page<ProductDO> objects = PageHelper.startPage(page, limit).doSelectPage(() -> productService.findAll().stream().map(productDO -> {
            CategoryDO categoryDO = categoryService.findById(productDO.getCategoryId());
//            System.out.println(categoryDO.toString());
            productDO.setCategoryDO(categoryDO);
            return productDO;
        }).collect(Collectors.toList()));
*/
        com.github.pagehelper.Page<ProductDO> objects = PageHelper.startPage(page, limit).doSelectPage(() -> productService.findAll());

//        List<String> categoryIds = productService.findAll().stream().map(ProductDO::getCategoryId).distinct().peek(System.out::println).collect(Collectors.toList());
//
//        Map<String, CategoryDO> collect = categoryService.findByIds(categoryIds).stream().collect(Collectors.toMap(CategoryDO::getId, categoryDO -> categoryDO));
//
//        System.out.println(collect.toString());

        //组装进category
//        List<Product> products = productService.findAll().stream().map(productDO -> {
//            CategoryDO categoryDO = categoryService.findById(productDO.getCategoryId());
//            Product product = new Product(productDO);
//            product.setCategory(categoryDO);
//            return product;
//        }).collect(Collectors.toList());

        long end = System.currentTimeMillis();

        System.out.println(end-begin);
        return ResultUtil.success(new Paging<ProductDO>(objects.getPageSize(),objects.getPageNum(),objects.getPages(),objects.getTotal(),objects.getResult()));
    }

    @ApiOperation("插入商品")
    @PostMapping("/save")
    public Result save(@RequestBody ProductDO productDO, HttpSession session) {
        if (productDO == null) {
            return ResultUtil.paramError();
        }

        return ResultUtil.success(productService.save(productDO, session));
    }

    @ApiOperation("更新商品")
    @PostMapping("/update")
    public Result update(@RequestBody ProductDO productDO) {
        if (productDO == null) {
            return ResultUtil.paramError();
        }
        return ResultUtil.success(productService.update(productDO));
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/del/{id}")
    public Result delete(@PathVariable("id") String id) {
        if (StringUtils.isBlank(id)) {
            return ResultUtil.paramError();
        }
        return ResultUtil.success(productService.DeleteById(id));
    }

    @ApiOperation("分页查询")
    @PostMapping("/page")
    public Result page(@RequestBody ProductParam param) {

        if (param == null) {
            param = new ProductParam();
        }
        if (param.getPageNumber() == null || param.getPageNumber() < 0) {
            param.setPageNumber(0);
        }
        if (param.getPageSize() == null || param.getPageSize() < 0) {
            param.setPageSize(10);
        }
        Page<ProductDO> page = new Page<>(param.getPageNumber(), param.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("category_id", param.getCategoryId());
        IPage<ProductDO> productDOIPage = productService.selectPage(page, queryWrapper);

        return ResultUtil.success(productDOIPage);
    }

    @ApiOperation("模糊搜索")
    @PostMapping("/search")
    public Result search(@RequestBody ProductParam param) {
        if (param == null) {
            return ResultUtil.paramError();
        }
        return ResultUtil.success(productService.search(param));
    }

    @ApiOperation("上下架")
    @GetMapping("/upOrDown/{id}")
    public Result search(@PathVariable("id") String id) {
        if (StringUtils.isBlank(id)) {
            return ResultUtil.paramError();
        }
        return ResultUtil.success(productService.upOrDown(id));
    }
    @ApiOperation("热销前五")
    @GetMapping("/hot")
    public Result hot(){
        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.orderByDesc("perchase_num");

        IPage page = productService.selectPage(new Page<ProductDO>(1, 5), queryWrapper);
        return ResultUtil.success(page);

    }
}

